/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : my_pw.js                            
*  Description  : 마이홈 비밀번호변경
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-14  김효순    Created.  
*  2012-05-14  김효순    Complete.
*******************************************************************************/
jQuery(function($){
	// 비밀번호
	function clsPasswd()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "비밀번호를 입력해주세요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsPasswd.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content li:eq(0)");
			$layer.find("input:text").after("<input type='password' />").next("input:password").attr({
				"class": $layer.find("input:text").attr("class")
				, "style": $layer.find("input:text").attr("style")
			}).end().remove();
			var $layer = $layer.find("input:password");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.val();
		}
		, getError: function(){
			if(this.errMsg == null) return null;
			else return this._ERRMSG[this.errMsg];
		}
		, isValid: function(){
			var $layer = this._HTMLDom["$layer"];
			
			this.errMsg = null;
			if(this.getValue().length == 0){
				this.errMsg = "_NOVALUE";
				return false;
			}
			
			return true;
		}
		, focus: function(){
			this._HTMLDom["$layer"].trigger("focus");
		}
	}
	
	// 새비밀번호
	function clsNewPasswd()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "새 비밀번호를 입력해주세요."
			, "_INVALID": "비밀번호는 4~12이하 영문 또는 숫자만 가능합니다."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsNewPasswd.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content li:eq(1)");
			$layer.find("input:text").after("<input type='password' />").next("input:password").attr({
				"class": $layer.find("input:text").attr("class")
				, "style": $layer.find("input:text").attr("style")
			}).end().remove();
			var $layer = $layer.find("input:password");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.val();
		}
		, getError: function(){
			if(this.errMsg == null) return null;
			else return this._ERRMSG[this.errMsg];
		}
		, isValid: function(){
			var $layer = this._HTMLDom["$layer"];
			var reg = /^[a-zA-Z0-9]{4,12}$/ig;
			
			this.errMsg = null;
			if(this.getValue().length == 0){
				this.errMsg = "_NOVALUE";
				return false;
			}else if(!reg.test(this.getValue())){
				this.errMsg = "_INVALID";
				return false;
			}
			
			return true;
		}
		, focus: function(){
			this._HTMLDom["$layer"].trigger("focus");
		}
	}
	
	// 비밀번호 확인
	function clsNewPasswdConfirm()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "새 비밀번호 확인을 입력해주세요."
			, "_INVALID": "비밀번호가 일치하지 않습니다."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsNewPasswdConfirm.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content li:eq(2)");
			$layer.find("input:text").after("<input type='password' />").next("input:password").attr({
				"class": $layer.find("input:text").attr("class")
				, "style": $layer.find("input:text").attr("style")
			}).end().remove();
			var $layer = $layer.find("input:password");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.val();
		}
		, getError: function(){
			if(this.errMsg == null) return null;
			else return this._ERRMSG[this.errMsg];
		}
		, isValid: function(cValue){
			var $layer = this._HTMLDom["$layer"];
			
			this.errMsg = null;
			if(this.getValue().length == 0){
				this.errMsg = "_NOVALUE";
				return false;
			}else if(this.getValue() != cValue){
				this.errMsg = "_INVALID";
				return false;
			}
			
			return true;
		}
		, focus: function(){
			this._HTMLDom["$layer"].trigger("focus");
		}
	}
	
	// 서버통신
	function clsComm()
	{
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){

		}
		, passwdSubmit: function(arg){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				var rnd = document.objGlobal.random(0, 1);
				var ret = {
					"결과코드": rnd
					, "메시지": null
				}
				document.objControl.finishPasswdSubmit(ret);
			}, 1500);
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objPasswd = null;
		this.objNewPasswd = null;
		this.objNewPasswdConfirm = null;

		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			this.objPasswd = new clsPasswd();
			this.objNewPasswd = new clsNewPasswd();
			this.objNewPasswdConfirm = new clsNewPasswdConfirm();
			
			$("div#container input:password").keydown(function(event){
				if(event.keyCode == 13) return false;
			});
			
			this._bind();
		}
		, _bind: function(){
			var thisClass = this;
			
			// 전송
			$("div#container img[alt='확인']").parent("a").click(function(){
				if(!thisClass.objPasswd.isValid()){
					alert(thisClass.objPasswd.getError());
					thisClass.objPasswd.focus();
					return;
				}else if(!thisClass.objNewPasswd.isValid()){
					alert(thisClass.objNewPasswd.getError());
					thisClass.objNewPasswd.focus();
					return;
				}else if(!thisClass.objNewPasswdConfirm.isValid(thisClass.objNewPasswd.getValue())){
					alert(thisClass.objNewPasswdConfirm.getError());
					thisClass.objNewPasswdConfirm.focus();
					return;
				}
				
				document.objGlobal.objModalLoading.open("sending...");
				thisClass.objComm.passwdSubmit({
					"비밀번호": thisClass.objPasswd.getValue()
					, "새비밀번호": thisClass.objNewPasswd.getValue()
				});
			});
		}
		// 전송완료
		, finishPasswdSubmit: function(ret){
			document.objGlobal.objModalLoading.close();
			if(ret["결과코드"]){
				alert("정보수정이 완료되었습니다.");
			}else{
				alert("비밀번호가 일치하지 않습니다.");
				this.objPasswd.focus();
			}
		}
	}
	
	document.objControl = new clsControl();
});
